﻿using Ortodoctor.modelo;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Ortodoctor.Funcionario
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpContext.Current.Session["USUARIO"] = null;
            HttpContext.Current.Session["USUARIO_ACESSO"] = null;
            HttpContext.Current.Session["ID_USUARIO_ACESSO"] = null;
            HttpContext.Current.Session["ID_CLINICA"] = null;

            if (!new Servico().verificaPrimeiroAcessoDia())
            {
                Session["DATA"] = DateTime.Now.AddDays(-1).ToString("dd/MM/yyyy");
                Session["alerta"] = "ERRO";
            }
            else
            {
                Session["DATA"] = DateTime.Now.ToString("dd/MM/yyyy");
                Session["alerta"] = null;
            }
        }

        [WebMethod]
        public static string autenticaUsuario(string login, string senha)
        {
            modeloUsuario usuario = new modeloUsuario();
            DataTable dt = usuario.autenticaUsuario(login, senha);
            if (dt.Rows.Count > 0)
            {
                HttpContext.Current.Session["USUARIO"] = login;
                HttpContext.Current.Session["USUARIO_ACESSO"] = dt.Rows[0]["nivel_acesso"];
                HttpContext.Current.Session["ID_USUARIO_ACESSO"] = dt.Rows[0]["id_usuario"];
                HttpContext.Current.Session["ID_CLINICA"] = dt.Rows[0]["id_clinica"];

                return "OK";
            }
            return "ERRO";
        }
    }
}